﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace sprocket
{
	public partial class SQLCredsDlg : Form
	{
		private string _stateUserName = string.Empty;
		private string _statePassword = string.Empty;
		private bool _stateUseSqlCreds = false;

		public SQLCredsDlg()
		{
			InitializeComponent();
		}

		public new DialogResult ShowDialog(IWin32Window owner)
		{
			DialogResult result = DialogResult.None;

			_stateUseSqlCreds = chkUseSQLCreds.Checked;
			_stateUserName = txtUserName.Text;
			_statePassword = txtPassword.Text;

			result = base.ShowDialog( owner );

			if( result == DialogResult.Cancel )
			{
				chkUseSQLCreds.Checked = _stateUseSqlCreds;
				txtUserName.Text = _stateUserName;
				txtPassword.Text = _statePassword;
			}

			_stateUserName = null;
			_statePassword = null;

			return result;
		}

		public string UserName
		{
			get { return txtUserName.Text; }
			set { txtUserName.Text = value; }
		}

		public string Password
		{
			get { return txtPassword.Text; }
			set { txtPassword.Text = value; }
		}

		public bool UseSQLServerCredentials
		{
			get { return  chkUseSQLCreds.Checked; }
			set { chkUseSQLCreds.Checked = value; }
		}

		public bool HaveValidCredentials
		{
			get { return !string.IsNullOrEmpty( txtUserName.Text ) &&
				!string.IsNullOrEmpty( txtPassword.Text ); }
		}

		public bool WantsCredentials
		{
			get
			{
				return this.UseSQLServerCredentials && this.HaveValidCredentials;
			}
		}

		private void chkUseSQLCreds_CheckedChanged(object sender, EventArgs e)
		{
			grpCreds.Enabled = chkUseSQLCreds.Checked;
		}
	}
}